﻿/********************************************************************
FileName    :   Bll_OS.cs
CreateTime  :   2020-11-17 02:19:44
BaseClass   :   Bll_OS
FileType    :   .cs
Author      :  minjie
*********************************************************************/


using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;

using System.Data;
namespace OS.DAL
{
    /// <summary>
    /// Dal_STDjLsh的摘要说明。
    /// </summary>
    public partial class Dal_STDjLsh
    {
        /// <summary> 
        /// 更新一条数据(字符串) 2016-07-13更新 
        ///   2020-04-09 更新 minjie 
        /// </summary> 
        public string UpdateStr(STDjLsh_Entity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update STDjLsh set ");

            if (model.DjName != null)
                strSql.Append(" DjName ='" + model.DjName + "',");

            if (model.DjLsh != null)
                strSql.Append(" DjLsh ='" + model.DjLsh + "'");


            strSql.Append(" where  DjName ='" + model.DjName + "' ");
            return strSql.ToString();
        }

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="DjName">主键</param>
        /// <returns></returns>
        public bool Exists(string DjName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from STDjLsh");
            strSql.Append(" where   DjName =@DjName ");
            SqlParameter[] parameters = { new SqlParameter("@DjName", SqlDbType.VarChar, 50) };
            parameters[0].Value = DjName;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary> 
        /// 增加一条数据 
        /// </summary> 
        public bool Add(STDjLsh_Entity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into STDjLsh (");
            strSql.Append("DjName, DjLsh) values (");
            strSql.Append("@DjName,@DjLsh) ");
            SqlParameter[] parameters = {

            new SqlParameter("@DjName",SqlDbType.VarChar,50),

            new SqlParameter("@DjLsh",SqlDbType.Int,4)
     };

            if (model.DjName != null)

                parameters[0].Value = model.DjName;
            else
                parameters[0].Value = DBNull.Value;

            if (model.DjLsh != null)

                parameters[1].Value = model.DjLsh;
            else
                parameters[1].Value = DBNull.Value;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary> 
        /// 增加一条数据(字符串) 
        /// </summary> 
        public string AddStr(STDjLsh_Entity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into STDjLsh(");
            strSql.Append("DjName, DjLsh) values (");
            strSql.Append("'" + model.DjName + "','" + model.DjLsh + "') ");
            return strSql.ToString();
        }

        public bool Update(string djName)
        {
            string sql = "update STDjLsh set djlsh=djlsh+1 where DjName='"+ djName + "' ";
            int rows = DbHelperSQL.ExecuteSql(sql);
            if (rows > 0)
                return true;
            else
                return false;
        }
        /// <summary> 
        /// 更新一条数据 
        ///创建时间:2020-04-09
        ///创建人:minjie
        /// </summary> 
        public bool Update(STDjLsh_Entity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update STDjLsh set ");
            strSql.Append(" DjName = @DjName,");

            strSql.Append(" DjLsh = @DjLsh");


            strSql.Append(" where  DjName =@DjName ");
            SqlParameter[] parameters = {

            new SqlParameter("@DjName",SqlDbType.VarChar,50),

            new SqlParameter("@DjLsh",SqlDbType.Int,4)
     };

            if (model.DjName != null)

                parameters[0].Value = model.DjName;
            else
                parameters[0].Value = DBNull.Value;

            if (model.DjLsh != null)

                parameters[1].Value = model.DjLsh;
            else
                parameters[1].Value = DBNull.Value;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary> 
        /// 删除一条数据 
        /// </summary> 
        public bool Delete(string DjName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from STDjLsh ");
            strSql.Append(" where   DjName =@DjName ");
            SqlParameter[] parameters = { new SqlParameter("@DjName", SqlDbType.VarChar, 50) };
            parameters[0].Value = DjName;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary> 
        /// 批量删除一批数据 
        /// </summary> 
        public bool DeleteList(string lists)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from sys_Config ");
            strSql.Append(" where DjName in (" + lists + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary> 
        /// 得到一个对象实体 
        /// </summary> 
        public STDjLsh_Entity GetModel(string djName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1  DjName, DjLsh ");
            strSql.Append(" from STDjLsh where DjName='" + djName + "'");
            STDjLsh_Entity model = new STDjLsh_Entity();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            DataTable dt = ds.Tables[0];
            return DataTableToEntity(dt);
        }
        /// <summary> 
        /// 将数据集(DataTable)转换 对象 STDjLsh_Entity
        /// 创建人：minjie 
        /// 创建时间:2019-02-18 02:05:58 
        /// </summary> 
        public STDjLsh_Entity DataTableToEntity(DataTable dt)
        {
            STDjLsh_Entity model = new STDjLsh_Entity();
            if (dt != null && dt.Rows.Count > 0)
            {
                model.DjName = dt.Rows[0]["DjName"].ToString();

                if (dt.Rows[0]["DjLsh"] != null && dt.Rows[0]["DjLsh"].ToString() != "")
                    model.DjLsh = int.Parse(dt.Rows[0]["DjLsh"].ToString());


                return model;
            }
            else
            {
                return null;
            }
        }



        /// <summary> 
        /// 执行sql语句  
        /// </summary> 
        public string GetSingle(string sql)
        {
            object obj = DbHelperSQL.GetSingle(sql);
            if (obj != null)
            {
                return obj.ToString();
            }
            else
                return "";
        }
        /// <summary> 
        /// 直接执行sql语句 
        /// </summary> 
        public int ExecuteSql(string sql)
        {
            return DbHelperSQL.ExecuteSql(sql);
        }



        /// <summary> 
        /// 直接执行sql语句 
        /// </summary> 
        public DataTable ExecuteDt(string sql)
        {
            return DbHelperSQL.ExecuteDt(sql);
        }

        /// <summary> 
        /// 获得记录条数 
        /// </summary> 
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) ");
            strSql.Append(" FROM STDjLsh ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj != null && obj.ToString() != "")
                return Convert.ToInt32(obj);
            else
                return 0;
        }
        /// <summary> 
        /// 批量处理  
        /// </summary> 
        public int ExecuteSqlTran(List<string> ls, ref string error)
        {
            return DbHelperSQL.ExecuteSqlTran(ls, ref error);
        }
        /// <summary> 
        /// 分页获取数据列表 
        /// </summary> 
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by " + orderby);
            }
            else
            {
                strSql.Append("order by T.DjName desc");
            }
            strSql.Append(")AS Row, T.*  from STDjLsh  T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary> 
        /// 获得数据列表 
        /// </summary> 
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM STDjLsh ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}
